#pragma once
#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *plock) : _plock(plock) {}
    ~Mutex() {}

    void lock()
    {
        if (_plock != nullptr)
            pthread_mutex_lock(_plock);
    }

    void unlock()
    {
        if (_plock != nullptr)
            pthread_mutex_unlock(_plock);
    }

private:
    pthread_mutex_t *_plock;
};

class LockGuard
{
public:
    LockGuard(Mutex mutex) : _mutex(mutex)
    {
        // 初始化LockGuard的时候进行加锁
        _mutex.lock();
    }

    ~LockGuard()
    {
        //  LockGuard销毁的时候进行解锁
        _mutex.unlock();
    }

private:
    Mutex _mutex;
};